package com.wkbb.sports.controller;

import com.github.pagehelper.PageInfo;
import com.wkbb.common.base.BaseController;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.StudentInfoDto;
import com.wkbb.common.dto.basic.UserDto;
import com.wkbb.common.redis.RedisTemplateUtil;
import com.wkbb.sports.feign.BasicFeignService;
import com.wkbb.sports.model.*;
import com.wkbb.sports.service.DeviceBindService;
import com.wkbb.sports.service.StudentArenaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(description = "用户设备绑定")
@RequestMapping("/sports/{apiVersion}/device/bind")
@Slf4j
public class DeviceBindController extends BaseController {

    @Autowired
    private DeviceBindService service;

    @ApiOperation(value = "绑定设备")
    @PostMapping(value = "/bindDevice")
    public ResultDTO<HlifeDeviceBind> bindDevice(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
                                               @ApiParam(value = "设备地址", required = true) @RequestParam(name = "macAddress", required = true) String macAddress,
                                               @ApiParam(value = "设备名称", required = true) @RequestParam(name = "name", required = true) String name) {
        try {
            UserDto userDto = this.getRouteUserByUnion();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            HlifeDeviceBind info = new HlifeDeviceBind();
            info.setUserId(userDto.getId());
            info.setName(name);
            info.setMacAddress(macAddress);
            info.setProjectKind(1);
            return service.bindDevice(info);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

    @ApiOperation(value = "解除绑定设备")
    @PostMapping(value = "/unbindDevice")
    public ResultDTO<HlifeDeviceBind> unbindDevice(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
                                               @ApiParam(value = "编号", required = true) @RequestParam(name = "id", required = true) Long id) {
        try {
            UserDto userDto = this.getRouteUserByUnion();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            service.deleteByPrimaryKey(id);
            return ResultDTO.successResult();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

    @ApiOperation(value = "已绑定设备列表")
    @GetMapping(value = "/getDeviceList")
    public ResultDTO<List<HlifeDeviceBind>> getDeviceList(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion) {
        try {
            UserDto userDto = this.getRouteUserByUnion();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            Map<String,Object> params = new HashMap<>();
            params.put("userId",userDto.getId());
            return new ResultDTO<>(service.getListByMapParams(params));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

}
